package com.wxm.common.file.start.controller;

import com.wxm.common.file.start.R;
import com.wxm.common.file.core.util.FileUtils;
import com.wxm.common.file.core.result.UploadFileResult;
import com.wxm.common.file.core.service.ImgUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * <p>
 *
 * </p>
 *
 * @author: wxm
 * @Date: 2022/1/20
 */
@RestController
@RequestMapping("/file/upload")
public class FileUploadController {
    @Autowired
    private ImgUploadService imgUploadService;

    @PostMapping("/img-multipart-file")
    public R<UploadFileResult> file(@RequestParam("file") MultipartFile file){
        UploadFileResult upload = imgUploadService.upload(0,file,"A","B");
        return R.ok(upload);
    }

    @PostMapping("/img-path")
    public R<UploadFileResult> filePath(String filePath){
        UploadFileResult upload = imgUploadService.upload(0,filePath,"A","B");
        return R.ok(upload);
    }

    @PostMapping("/img-byte")
    public R<UploadFileResult> byByte(String filePath){
        File file = new File(filePath);
        UploadFileResult upload = imgUploadService.upload(0, FileUtils.getBytes(file),file.getName(),"A","B");
        return R.ok(upload);
    }

    @PostMapping("/img-file")
    public R<UploadFileResult> byFile(String filePath){
        File file = new File(filePath);
        UploadFileResult upload = imgUploadService.upload(0,file,"A","B");
        return R.ok(upload);
    }

    @PostMapping("/img-input-stream")
    public R<UploadFileResult> inputStream(String filePath) throws FileNotFoundException {
        File file = new File(filePath);
        UploadFileResult upload = imgUploadService.upload(0,new FileInputStream(file),file.getName(),"A","B");
        return R.ok(upload);
    }

    @PostMapping("/img-delete")
    public R<String> imgDelete(String filePath){
        imgUploadService.delete(0,filePath);
        return R.ok("删除成功");
    }
}
